<?php

class ContactController extends Controller{
	

	public $models = array("Pages");

	public function index(){
		
		$this->assign("title",$this->template_parser->get_config_vars("contact"));
		$contactpage = $this->Pages->findRow("html",array("id"=>2));
		$contactpage["id"] = 2;
		$this->translateRow($contactpage, "pages");
		$this->assign("contactpage", $contactpage["html"]);

		$this->display("contact/index.html");
	}
	
	public function send() {
		$hash = $this->session_get("CAPTCHA_HASH");
		$status = 0;
		$message = $this->template_parser->get_config_vars("contact_sent");;
		
		if (strtolower($this->data["captcha"]) != $hash) {
			$status = 1;
			$message = $this->template_parser->get_config_vars("captcha_wrong");
		}elseif(!strlen($this->data["name"]) || !strlen($this->data["email"]) || !strlen($this->data["message"])) {
			$status = 1;
			$message = $this->template_parser->get_config_vars("contact_all_required");
		}else if(!$this->_is_email($this->data["email"])) {
			$status = 1;
			$message = $this->template_parser->get_config_vars("contact_invalid_email");
		}else{
			$to = $this->template_parser->get_config_vars("email");
			$subject = "Kontakt sa sajta: {$this->data["name"]} - ".date("d.m.Y");
			$msg = $this->data["message"];
			$headers = "From: \"{$this->data["name"]}\"<{$this->data["email"]}>";
			if(!mail($to, $subject, $msg, $headers)) {
				$status  = 1;
				$message = $this->template_parser->get_config_vars("contact_failed");
			}
		}
		header("X-Contact-Status: $status");
		echo $message;
	}
	
	private function _is_email($email) {
		$ret = preg_match('/^[_\.a-z0-9]+@[a-z0-9_]+(\.[a-z0-9_]+)+$/i', $email) ?  true : false;
		return $ret;
	}
}

?>